Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
Cu.import("resource://app/jsmodules/sbProperties.jsm");

var batchWindow;

var LM_batchWin= new function()
{

function lm_launchBatch(items) {

  var windowWatcherService = Components.classes['@mozilla.org/embedcomp/window-watcher;1'].getService(Components.interfaces.nsIWindowWatcher);

  batchWindow = windowWatcherService.openWindow(null, "chrome://lyricmaster/content/lm_fetcherwindow.xul", "lyricmaster", "chrome,centerscreen,modal=no", null);
  
  
  batchWindow.addEventListener("DOMContentLoaded", function() {
     LM_batchWin.build_window(items);
  }, false);
}

this.lm_launchBatch = lm_launchBatch;

function bWin_update(lyrics, list, index)
{
	if (batchWindow.document)
	{	

	if ((lyrics.length > 10) && (lyrics != ""))
	{
	   var set_cbox = batchWindow.document.getElementById(list[index].c_box);
	   set_cbox.setAttribute("checked", "true");
	   var img_src = batchWindow.document.getElementById(list[index].i_box);
	   img_src.setAttribute("src", "chrome://songbird/skin/base-elements/icon-success.png");
	   var lyric_data = batchWindow.document.getElementById(list[index].t_box);
	   lyric_data.value = lyrics;
	}
	else
	{
	   var img_src = batchWindow.document.getElementById(list[index].i_box);
	   img_src.setAttribute("src", "chrome://songbird/skin/base-elements/icon-error.png");
	}

	if (list[index + 1])
		lyricmasterFetcher.fetch_lyr(list[index + 1].art_dat, list[index + 1].trk_dat, function(lyrics){bWin_update(lyrics, list, index + 1)})

	}
}

function start_fetch(items){

	var thingsToFetch = [];

	for (var i = 0; i < items.length; i++)
	{

	thingsToFetch[i] = {art_dat: items[i].getProperty("http://songbirdnest.com/data/1.0#artistName"), trk_dat: items[i].getProperty("http://songbirdnest.com/data/1.0#trackName"), c_box: items[i] + "-c", i_box: items[i] + "-i", t_box: items[i] + "-t"}

	}	

	lyricmasterFetcher.fetch_lyr(thingsToFetch[0].art_dat, thingsToFetch[0].trk_dat, function(lyrics){bWin_update(lyrics, thingsToFetch, 0)});


}


function batchSave(items){

	var mediaItemArray = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);


	for (var i = 0; i < items.length; i++)
	{

	var chk_box = batchWindow.document.getElementById((items[i] + "-c"));
	ifTrue = chk_box.getAttribute("checked");

	if (ifTrue == "true")
	{
		txt_box = items[i] + "-t";
		sav_lyrics = batchWindow.document.getElementById(txt_box).value;
		items[i].setProperty("http://songbirdnest.com/data/1.0#lyrics", sav_lyrics);
		mediaItemArray.appendElement(items[i], false);

	}

	}


	if (mediaItemArray.length > 0) 
	{

		var metadataService = Cc["@songbirdnest.com/Songbird/FileMetadataService;1"].getService(Ci.sbIFileMetadataService);
 
		// This will write out the properties in propArray for each item.
		var propArray = ArrayConverter.stringEnumerator([SBProperties.lyrics]);
		metadataService.write(mediaItemArray, propArray);
	}


	batchWindow.close();

}


function build_window(items){

	sav_btn = batchWindow.document.getElementById("btn_save");
	sav_btn.addEventListener('click', function(){ batchSave(items)}, false);


	for (var i = 0; i < items.length; i++)
	{

	var art_dat = items[i].getProperty("http://songbirdnest.com/data/1.0#artistName");
	var trk_dat = items[i].getProperty("http://songbirdnest.com/data/1.0#trackName");
	var lm_row = document.createElement("row");
	lm_row.setAttribute("align", "center");
	var lm_cbox = document.createElement("checkbox");
	lm_cbox.setAttribute("id", items[i] + "-c");
	lm_cbox.setAttribute("style", "padding:5px");
	var lm_image = document.createElement("image");
	lm_image.setAttribute("id", items[i] + "-i");
	lm_image.setAttribute("src", "chrome://songbird/skin/base-elements/icon-loading.png");
	lm_image.setAttribute("style", "padding:5px");
	var lm_art = document.createElement("description");
	lm_art.appendChild(document.createTextNode(art_dat));
	var lm_trk = document.createElement("description");
	lm_trk.appendChild(document.createTextNode(trk_dat));
	var lm_tbox = document.createElement("textbox");
	lm_tbox.setAttribute("rows", "3");
	lm_tbox.setAttribute("cols", "50");
	lm_tbox.setAttribute("multiline", "true");
	lm_tbox.setAttribute("id", items[i] + "-t");
	
	lm_row.appendChild(lm_cbox);
	lm_row.appendChild(lm_image);
	lm_row.appendChild(lm_art);
	lm_row.appendChild(lm_trk);
	lm_row.appendChild(lm_tbox);

	sep = document.createElement("separator");
	sep.setAttribute("flex", "1");
	sep.setAttribute("style", "padding-top:5px");
	sep.setAttribute("style", "padding-bottom:5px"); 


	var batch_data = batchWindow.document.getElementById('batch_data');
	batch_data.appendChild(lm_row);
	batch_data.appendChild(sep);




	}

	start_fetch(items);
}


this.build_window = build_window;

};
